package com.xialing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xialing.config.InterceptorConfig;
import com.xialing.controller.request.LinkGroupAddRequest;
import com.xialing.controller.request.LinkGroupUpdateRequest;
import com.xialing.interceptor.LoginInterceptor;
import com.xialing.manager.LinkGroupManager;
import com.xialing.mapper.LinkGroupMapper;
import com.xialing.model.LinkGroupDO;
import com.xialing.service.LinkGroupService;
import com.xialing.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 短链接分组表 服务实现类
 * </p>
 *
 * @author 二当家小D
 * @since 2025-10-20
 */
@Service
public class LinkGroupServiceImpl extends ServiceImpl<LinkGroupMapper, LinkGroupDO> implements LinkGroupService {


    @Autowired
    private LinkGroupManager linkGroupManager;


    /**
     * 添加分组
     * @param linkGroupAddRequest
     * @return
     */
    @Override
    public int add(LinkGroupAddRequest linkGroupAddRequest) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setTitle(linkGroupAddRequest.getTitle());

        int rows = linkGroupManager.add(linkGroupDO);

        return rows;
    }

    /**
     * 删除分组
     * @param groupId
     * @return
     */
    @Override
    public int delete(Long groupId) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        int rows = linkGroupManager.delete(accountNo,groupId);
        return rows;
    }

    @Override
    public LinkGroupVO detail(Long groupId) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(accountNo,groupId);
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;
    }


    @Override
    public List<LinkGroupVO> listAllGroup() {

        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList = linkGroupManager.listAllGroup(accountNo);
        List<LinkGroupVO> linkGroupVOList = linkGroupDOList.stream().map(obj -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(obj,linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());

        return linkGroupVOList;

    }

    @Override
    public int updateById(LinkGroupUpdateRequest linkGroupUpdateRequest) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setId(linkGroupUpdateRequest.getId());
        linkGroupDO.setTitle(linkGroupUpdateRequest.getTitle());
        linkGroupDO.setAccountNo(accountNo);
        int rows = linkGroupManager.updateById(linkGroupDO);

        return rows;
    }
}
